package com.cens.mall.order.listener;

import com.cens.common.to.SeckillOrderTO;
import com.cens.mall.order.service.OrderService;
import com.cens.mall.order.to.OrderCreateTo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = "order-seckill-queue")
public class OrderSeckillListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listene(SeckillOrderTO order, Channel cHannel, Message message) throws IOException {
        try {
            orderService.createSeckillOrder(order);
            cHannel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            cHannel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
